package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.MyPostsBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class HisPostsPresenter extends basePresenter {

    private Contract.HisPostsView myPostsView;

    public HisPostsPresenter(Contract.HisPostsView view) {
        super(view);
        this.myPostsView = view;
    }
    public void initData(int page, String userid,final boolean isRefresh, boolean isShowDiaolg) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.userActivity);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("page", page + "");
        rp.addBodyParameter("userid",userid);
        x.http().post(rp, new myCallback(BaseActivity.context, MyPostsBean.class, isShowDiaolg, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                MyPostsBean bean = (MyPostsBean) baseBean;
                if (isRefresh) {
                    myPostsView.onRefresh(bean);
                } else {
                    myPostsView.onLoadMore(bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                myPostsView.onFail();
            }
        });
    }

    public void attention(String userid, final int position, int action) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.attentionAction);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("userid", userid);
        rp.addBodyParameter("action", action + "");
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                myPostsView.attentionedSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }
}
